package com.cz.nongyetongv5.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.JsonUtil;
import com.cz.nongyetongv5.bean.PatrolContent;
import com.cz.nongyetongv5.bean.PatrolContentBean;
import com.cz.nongyetongv5.bean.PatrolContentResponse;
import com.cz.nongyetongv5.bean.PatrolListResponse;
import com.cz.nongyetongv5.bean.PatrolPic;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class PatrolContentDetailActivity extends Activity implements View.OnClickListener {
    private RelativeLayout btnBack;
    private RecyclerView recyclerView;
    private PatrolContentAdapter adapter;
    private View notDataView;
    private List<List<PatrolContent>> patrolContents;
//    private List<PatrolContent> checkContents;
//    private PatrolContentResponse patrolContentResponse;
//    private PatrolContentBean patrolContentBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patrol_content_detail);
        btnBack = findViewById(R.id.btnBack);
        btnBack.setOnClickListener(this);
        patrolContents=new ArrayList<>();
        recyclerView = findViewById(R.id.lv_patrol_content);
        if (getIntent().getSerializableExtra("checkContent") != null) {
            patrolContents.addAll((List<List<PatrolContent>>) getIntent().getSerializableExtra("checkContent"));
        }
        adapter = new PatrolContentAdapter();
        notDataView = getLayoutInflater().inflate(R.layout.view_nodata, (ViewGroup) recyclerView.getParent(), false);
        recyclerView.setLayoutManager(new LinearLayoutManager(PatrolContentDetailActivity.this));
        adapter.setEmptyView(notDataView);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnBack:
                finish();
                break;
        }
    }


    public class PatrolContentAdapter extends BaseQuickAdapter<List<PatrolContent>, BaseViewHolder> {
        public PatrolContentAdapter() {
            super(R.layout.patrol_content_item, patrolContents);
        }

        @Override
        protected void convert(BaseViewHolder viewHolder, final List<PatrolContent> item) {
            if (item.get(0).getType().equals("1")) {
                viewHolder.setText(R.id.tv_hit, item.get(0).getType_name()).setTextColor(R.id.tv_hit, Color.parseColor("#3f90ff"));
                viewHolder.setImageResource(R.id.iv_icon, R.drawable.icon_xcnr01);
            } else if (item.get(0).getType().equals("2")) {
                viewHolder.setText(R.id.tv_hit, item.get(0).getType_name()).setTextColor(R.id.tv_hit, Color.parseColor("#ffa23f"));
                viewHolder.setImageResource(R.id.iv_icon, R.drawable.icon_xcnr02);
            } else if (item.get(0).getType().equals("3")) {
                viewHolder.setText(R.id.tv_hit, item.get(0).getType_name()).setTextColor(R.id.tv_hit, Color.parseColor("#21b481"));
                viewHolder.setImageResource(R.id.iv_icon, R.drawable.icon_xcnr03);
            } else if (item.get(0).getType().equals("4")) {
                viewHolder.setText(R.id.tv_hit, item.get(0).getType_name()).setTextColor(R.id.tv_hit, Color.parseColor("#fd5f7a"));
                viewHolder.setImageResource(R.id.iv_icon, R.drawable.icon_xcnr04);
            }
            RecyclerView lv_content = viewHolder.getView(R.id.lv_content);
            PatrolContentItemAdapter adapter = new PatrolContentItemAdapter(item);
            notDataView = getLayoutInflater().inflate(R.layout.view_nodata, (ViewGroup) recyclerView.getParent(), false);
            lv_content.setLayoutManager(new LinearLayoutManager(PatrolContentDetailActivity.this));
            lv_content.setAdapter(adapter);
        }
    }

    public class PatrolContentItemAdapter extends BaseQuickAdapter<PatrolContent, BaseViewHolder> {
        public PatrolContentItemAdapter(List<PatrolContent> patrolContentList) {
            super(R.layout.patrol_content_item_item, patrolContentList);
        }

        @Override
        protected void convert(BaseViewHolder viewHolder, final PatrolContent item) {
            viewHolder.setText(R.id.tv_text, item.getName()).addOnClickListener(R.id.tv_select).addOnClickListener(R.id.tv_none);
            if (item.getIs_have() != null && item.getIs_have().equals("1")) {
                viewHolder.setTextColor(R.id.tv_select, Color.parseColor("#ffffff"));
                viewHolder.setBackgroundRes(R.id.tv_select, R.drawable.bt_content_radius_s);
                viewHolder.setTextColor(R.id.tv_none, Color.parseColor("#585858"));
                viewHolder.setBackgroundRes(R.id.tv_none, R.drawable.bt_content_radius);
            } else if (item.getIs_have() != null && item.getIs_have().equals("2")) {
                viewHolder.setTextColor(R.id.tv_select, Color.parseColor("#585858"));
                viewHolder.setBackgroundRes(R.id.tv_select, R.drawable.bt_content_radius);
                viewHolder.setTextColor(R.id.tv_none, Color.parseColor("#ffffff"));
                viewHolder.setBackgroundRes(R.id.tv_none, R.drawable.bt_content_radius_s);
            } else {
                viewHolder.setTextColor(R.id.tv_select, Color.parseColor("#585858"));
                viewHolder.setBackgroundRes(R.id.tv_select, R.drawable.bt_content_radius);
                viewHolder.setTextColor(R.id.tv_none, Color.parseColor("#585858"));
                viewHolder.setBackgroundRes(R.id.tv_none, R.drawable.bt_content_radius);
            }

            viewHolder.setText(R.id.tv_select, item.getLeft());
            viewHolder.setText(R.id.tv_none, item.getRight());
            RelativeLayout rl_patrol_pic = viewHolder.getView(R.id.rl_patrol_pic);
            if (item.getType().equals("4")) {
                if (item.getIs_have().equals("0") || item.getIs_have().equals("2")) {
                    rl_patrol_pic.setVisibility(View.GONE);
                } else {
                    rl_patrol_pic.setVisibility(View.VISIBLE);
                    RecyclerView lv_content_pic = viewHolder.getView(R.id.lv_content_pic);

                    PhotoAdapter adapter = new PhotoAdapter(item.getCheckContentPhoto());
//                    notDataView = getLayoutInflater().inflate(R.layout.view_nodata, (ViewGroup) recyclerView.getParent(), false);
                    lv_content_pic.setLayoutManager(new GridLayoutManager(PatrolContentDetailActivity.this, 3));
                    lv_content_pic.setAdapter(adapter);
                    adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                            Intent intent = new Intent(PatrolContentDetailActivity.this, PicDetailActivity.class);
                            intent.putExtra("position", position);
                            intent.putExtra("photos", (Serializable) item.getCheckContentPhoto());
                            startActivity(intent);
                        }
                    });
                }
            } else {
                rl_patrol_pic.setVisibility(View.GONE);
            }
        }
    }

    public class PhotoAdapter extends BaseQuickAdapter<PatrolPic, BaseViewHolder> {


        public PhotoAdapter(List<PatrolPic> picBeenList) {
            super(R.layout.product_pic_item, picBeenList);
        }

        @Override
        protected void convert(BaseViewHolder helper, PatrolPic item) {
            Glide.with(mContext)
                    .load(item.getAttach_url()).placeholder(R.drawable.loading)
                    .into((ImageView) helper.getView(R.id.iv_iv));

        }
    }
}
